# Import the datetime module to handle date and time objects.
import datetime

# Import the base Model class from Flask-AppBuilder, which this model will inherit from.
from flask_appbuilder import Model
# Import column types and the Column class from SQLAlchemy to define the database schema.
from sqlalchemy import BigInteger, Column, DateTime, Integer, String


# Define the BillingOrder class, which maps to a database table.
# This class inherits from flask_appbuilder.Model, integrating it with the F.A.B. framework.
class BillingOrder(Model):
    """
    Custom attributes attached to the user.

    Extending the user attribute is tricky due to its dependency on the
    authentication typew an circular dependencies in Myapp. Instead, we use
    a custom model for adding attributes.

    """

    # Specifies the name of the database table for this model.
    __tablename__ = 'billing_order'
    # Defines the primary key column for the table.
    id = Column(Integer, primary_key=True)  # pylint: disable=invalid-name
    # Defines the 'username' column to store the name of the user associated with the order.
    # It is a string with a maximum length of 64 characters and cannot be null.
    username = Column(String(64), nullable=False)
    # Defines the 'order_id' column to store a unique identifier for the billing order.
    # It is a string with a maximum length of 128 characters and cannot be null.
    order_id = Column(String(128), nullable=False)
    # Defines the 'bill_type' column to categorize the type of the order.
    # It is an integer, cannot be null, and has a server-side default of 0.
    bill_type = Column(Integer, nullable=False, server_default='0', default=0, comment='订单类型')
    # Defines the 'status' column to track the current status of the order (e.g., pending, completed).
    # It is an integer, cannot be null, and has a server-side default of 0.
    status = Column(Integer, nullable=False, server_default='0', default=0, comment='订单状态')
    # Defines the 'duration' column, likely for the service duration associated with the order.
    # It uses BigInteger to accommodate large values, cannot be null, and defaults to 0.
    duration = Column(
        BigInteger, nullable=False, server_default='0', default=0, comment='持续时间'
    )
    # Defines the 'price' column for the unit price of the service or item.
    # It is an integer, cannot be null, and defaults to 0.
    price = Column(Integer, nullable=False, server_default='0', default=0, comment='单价')
    # Defines the 'amount' column for the total cost of the order.
    # It uses BigInteger, cannot be null, and defaults to 0.
    amount = Column(BigInteger, nullable=False, server_default='0', default=0, comment='金额')
    # Defines the 'ext_info' column to store additional information, likely in JSON format.
    # It is a string with a max length of 1024 and defaults to an empty JSON object '{}'.
    ext_info = Column(String(1024), default='{}', server_default='{}', comment='扩展信息')
    # Defines the 'create_time' column to record when the order was created.
    # It is a DateTime column that defaults to the current timestamp and can be null.
    create_time = Column(DateTime, default=datetime.datetime.now, nullable=True)
    # Defines the 'update_time' column to record the last time the order was modified.
    # It is a DateTime column that defaults to the current timestamp and can be null.
    update_time = Column(DateTime, default=datetime.datetime.now, nullable=True)
